<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @cancel="handleCancel"
    cancelText="关闭">
    <template slot=" ">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button @click="handleOk" type="primary">提交</a-button>
    </template>

    <a-button @click="autoGenFieldMap" type="primary">自动导入</a-button>

    <a-spin :spinning="confirmLoading">
      <a-table ref="table" size="middle" :scroll="{x:true}" rowKey="id" :columns="columns"
               :dataSource="dataSource" :loading="loading" class="j-table-force-nowrap" :rowKey="(record,index)=>{return index}">
        <template #shuru>-></template>
        <template #operate='text,record'><a-button @click="deleteFieldMap(record.inputField)">删除</a-button></template>
        <template #paramValue='text,record'> <a-input :width='100' v-model="record.outputField"/></template>

      </a-table>
    </a-spin>

  </j-modal>
</template>

<script>
import { genNodeParam,saveNodeFieldNode } from '@/api/apiDs'

export default {
  name: '流程节点字段映射',
  data() {
    return {
      nodeId: '',
      confirmLoading: false,
      loading: false,
      dataSource: [],
      title: '数据字段映射',
      visible: false,
      width: 500,
      columns: [
        {
          title: '输入字段',
          align: 'center',
          dataIndex: 'inputField',
          ellipsis: true
        },
        {
          title: '->',
          align: 'center',
          dataIndex: 'shuru',
          ellipsis: true,
          scopedSlots: { customRender: 'shuru' }
        },
        {
          title: '输出字段',
          align: 'center',
          dataIndex: 'outputField',
          ellipsis: true,
          scopedSlots: { customRender: 'paramValue' }
        },{
          title: '操作',
          align: 'center',
          dataIndex: 'operate',
          ellipsis: true,
          scopedSlots: { customRender: 'operate' }
        }]
    }
  },
  methods: {
    showTable(tid,fieldMapJson) {
      this.visible = true
      this.nodeId = tid
      if(fieldMapJson === ''){
        this.dataSource = []
      }else{
        let parse = JSON.parse(fieldMapJson)
        this.dataSource = parse
      }


    },

    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk(){
      let param = { fieldMaps: this.dataSource,flowNodeId: this.nodeId }
      saveNodeFieldNode(param).then((res) => {
        if(res.result){
          this.$message.success(res.message);
          this.$emit('ok');
          this.$emit("data-to-parent", JSON.stringify(this.dataSource));  //数据发送到父组件
        }else{
          this.$message.warning(res.message);
        }
        this.close()
      }).catch((err) => {
        this.$message.warning(err)
      })

    },
    autoGenFieldMap(){
      let params = {flowNodeId:this.nodeId}
      genNodeParam(params).then(res =>{
        if(res.success){
          this.dataSource = res.result
        }
      })
    },
    deleteFieldMap(record){
      console.log(record)
      let idx = this.dataSource.findIndex(item => item.inputField === record)
      if (idx !== -1) {
        this.$delete(this.dataSource, idx);
      }
    }
  }
}
</script>